package com.qilu.pe.ui.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.ItemSet;
import com.qilu.pe.dao.bean.OrderDetail;
import com.qilu.pe.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.dialog.ProgressHUD;
import com.ruitu.arad.util.ToastUtils;

import io.reactivex.functions.Consumer;

/**
 * <br/>┏------------------------------------------
 * <br/>│Note:
 * <br/>│
 * <br/>┣------------------------------------------
 * <br/>│                 /) /)
 * <br/>│               ฅ(• ﻌ •)ฅ
 * <br/>│  Created by @author HaoKing on 2020/11/02 9:22.
 * <br/>│          www.muchenxinxi.com
 * <br/>│           www.nextdever.com
 * <br/>│           WeChat: NextDever
 * <br/>│             QQ: 773693552
 * <br/>│     QQ Group: Geek Chat 344386592
 * <br/>┗------------------------------------------
 */
public class OrderDetailSetItemListAdapter extends ListBaseAdapter<OrderDetail.Combo.Info> {

    private String orderId;
    private String orderStatus = "";
    private static ProgressHUD mProgressHUD;

    public OrderDetailSetItemListAdapter(Context context) {
        super(context);
    }

    public OrderDetailSetItemListAdapter(Context context, String orderId, String orderStatus) {
        super(context);
        this.orderId = orderId;
        this.orderStatus = orderStatus;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_confirm_order_item_list;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        TextView tv_name = holder.getView(R.id.tv_name);
        TextView tv_time = holder.getView(R.id.tv_time);
        TextView tv_price = holder.getView(R.id.tv_price);
        final TextView vRefund = holder.getView(R.id.vRefund);
        final TextView vRefundStatus = holder.getView(R.id.vRefundStatus);

        OrderDetail.Combo.Info item = mDataList.get(position);

        tv_name.setText(item.getName());
        if (item.getDay() == null) {
            tv_time.setVisibility(View.INVISIBLE);
        } else {
            tv_time.setVisibility(View.VISIBLE);
            tv_time.setText(item.getDay() + "个工作日");
        }
        tv_price.setText("¥" + item.getPrice());

        if ("5".equals(orderStatus)) {
            if (item.getStatus() == 1) {
                vRefundStatus.setVisibility(View.VISIBLE);
                vRefundStatus.setText("退款中");
            } else if (item.getStatus() == 2) {
                vRefundStatus.setVisibility(View.VISIBLE);
                vRefundStatus.setText("已退款");
            } else {
                vRefund.setVisibility(View.VISIBLE);
                vRefund.setTag(item);
                vRefund.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        final NormalDialog dialog = new NormalDialog(mContext)
                                .isTitleShow(false)
                                .contentGravity(Gravity.CENTER)
                                .content("确定发起退款？");
                        dialog.setOnBtnClickL(new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                dialog.dismiss();
                            }
                        }, new OnBtnClickL() {
                            @Override
                            public void onBtnClick() {
                                dialog.dismiss();
                                showProgress();
                                APIRetrofit.getDefault().refundMyOrderItem(Global.HEADER, orderId, ((OrderDetail.Combo.Info) v.getTag()).getId())
                                        .compose(RxUtil.<BaseResult2>subIoObMain())
                                        .subscribe(new Consumer<BaseResult2>() {
                                            @Override
                                            public void accept(BaseResult2 res) throws Exception {
                                                hideProgress();
                                                if (res.isSuccess()) {
                                                    ToastUtils.showShort("申请退款成功");
                                                    vRefund.setVisibility(View.GONE);
                                                    vRefundStatus.setVisibility(View.VISIBLE);
                                                    vRefundStatus.setText("退款中");
                                                } else {
                                                    ToastUtils.showShort(res.getMsg());
                                                }
                                            }
                                        }, new Consumer<Throwable>() {
                                            @Override
                                            public void accept(Throwable throwable) throws Exception {
                                                hideProgress();
                                                throwable.printStackTrace();
                                                ToastUtils.showShort("退款失败");
                                            }
                                        });
                            }
                        });
                        dialog.show();
                    }
                });
            }
        }
    }

    // 显示一个等待dialog，内容交互或者提交的时候使用 true显示 false隐藏
    public ProgressHUD showProgress(boolean show) {
        mProgressHUD = showProgressWithText(show, "加载中...");
        return mProgressHUD;
    }

    // 显示一个等待dialog，内容交互或者提交的时候使用 是否要显示 msg 要显示的文字
    public ProgressHUD showProgressWithText(boolean show, String msg) {
        try {
            if (show) {
                mProgressHUD = ProgressHUD.show(mContext, msg, null);
            } else {
                if (mProgressHUD != null) {
                    mProgressHUD.dismiss();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mProgressHUD;
    }

    public ProgressHUD showProgress() {
        return showProgress(true);
    }

    public void hideProgress() {
        showProgress(false);
    }
}
